<script setup lang="tsx">
import { Form, FormSchema } from '@/components/Form'
import { PropType, reactive, ref } from 'vue'
import { useValidator } from '@/hooks/web/useValidator'
import { SysMachineInfoDetailVo } from '@/api/system/monitor/SysMachineInfo/types'
import { isBlank } from '@/utils/StringUtils'
import { useBaseWriteView } from '@/base/views/BaseWriteView'
import { sysDictChooseListApi } from '@/api/system/dict/SysDict'
import { KeyLabelValueVo } from '@/base/api/KeyLabelValueVo'
// [[ OnflowGenerator Range (- selectFunctionImport -) Start ]]

// [[ OnflowGenerator Range (- selectFunctionImport -) End ]]

const { required } = useValidator()

const props = defineProps({
  currentRow: {
    type: Object as PropType<Nullable<SysMachineInfoDetailVo>>,
    default: () => null
  },
  editType: {
    type: String,
    default: 'add'
  },
  editId: {
    type: Number,
    default: -1
  }
})
const isAdd = ref(props.editType === 'add')

const { formRegister, submit, doWatch } = useBaseWriteView(props)
doWatch()

const formRef = ref<typeof Form>()
// [[ OnflowGenerator Range (- dictFunctions -) Start ]]
const onlineStateOptions = ref<Array<KeyLabelValueVo>>([])
const getOnlineState = async () => {
  const res = await sysDictChooseListApi('sysMachineOnlineState')
  onlineStateOptions.value = res.data
}
getOnlineState()

// [[ OnflowGenerator Range (- dictFunctions -) End ]]

// [[ OnflowGenerator Range (- selectFunctions -) Start ]]

// [[ OnflowGenerator Range (- selectFunctions -) End ]]

const schema = reactive<FormSchema[]>([
  // [[ OnflowGenerator Range (- inputSchema -) Start ]]
  {
    field: 'machineId',
    label: '服务器id',
    component: 'Input',
    componentProps: {
      placeholder: '请输入服务器id',
      clearable: true
    }
  },
  {
    field: 'ipAddress',
    label: '服务器ip地址',
    component: 'Input',
    componentProps: {
      placeholder: '请输入服务器ip地址',
      clearable: true
    }
  },
  {
    field: 'macAddress',
    label: '服务器mac地址',
    component: 'Input',
    componentProps: {
      placeholder: '请输入服务器mac地址',
      clearable: true
    }
  },
  {
    field: 'lastReportDate',
    label: '上次上报时间',
    component: 'DatePicker',
    componentProps: {
      placeholder: '请选择上次上报时间',
      clearable: true,
      type: 'datetime',
      valueFormat: 'YYYY-MM-DD HH:mm:ss'
    }
  },
  {
    field: 'onlineState',
    label: '在线状态',
    value: '0',
    component: 'Select',
    componentProps: {
      placeholder: '请选择在线状态',
      clearable: true,
      filterable: true,
      options: onlineStateOptions
    }
  },
  {
    field: 'osName',
    label: '操作系统名称',
    component: 'Input',
    componentProps: {
      placeholder: '请输入操作系统名称',
      clearable: true
    }
  },
  {
    field: 'serverName',
    label: '服务器名称',
    component: 'Input',
    componentProps: {
      placeholder: '请输入服务器名称',
      clearable: true
    }
  },
  {
    field: 'sn',
    label: '序列号',
    component: 'Input',
    componentProps: {
      placeholder: '请输入序列号',
      clearable: true
    }
  },
  {
    field: 'cpuName',
    label: 'CPU名称',
    component: 'Input',
    componentProps: {
      placeholder: '请输入CPU名称',
      clearable: true
    }
  },
  {
    field: 'cpuFrequency',
    label: 'CPU频率',
    component: 'Input',
    componentProps: {
      placeholder: '请输入CPU频率',
      clearable: true
    }
  },
  {
    field: 'physicalProcessorCount',
    label: 'CPU物理核心数',
    value: '0',
    component: 'Input',
    componentProps: {
      placeholder: '请输入CPU物理核心数',
      clearable: true
    }
  },
  {
    field: 'logicalProcessorCount',
    label: 'CPU逻辑核心数',
    value: '0',
    component: 'Input',
    componentProps: {
      placeholder: '请输入CPU逻辑核心数',
      clearable: true
    }
  },
  {
    field: 'physicalMemory',
    label: '物理内存',
    value: '0',
    component: 'Input',
    componentProps: {
      placeholder: '请输入物理内存',
      clearable: true
    }
  },
  {
    field: 'virtualMemory',
    label: '虚拟内存',
    value: '0',
    component: 'Input',
    componentProps: {
      placeholder: '请输入虚拟内存',
      clearable: true
    }
  },
  {
    field: 'totalStorage',
    label: '总存储',
    value: '0',
    component: 'InputNumber',
    componentProps: {
      placeholder: '请选择总存储',
      clearable: true
    }
  },
  // [[ OnflowGenerator Range (- inputSchema -) End ]]
  {
    field: 'createTime',
    label: '创建时间',
    component: 'Input',
    remove: isAdd.value,
    componentProps: {
      placeholder: '暂无创建时间',
      disabled: true
    }
  },
  {
    field: 'updateTime',
    label: '更新时间',
    component: 'Input',
    remove: isAdd.value,
    componentProps: {
      placeholder: '暂无更新时间',
      disabled: true
    }
  }
])

const rules = reactive({
  // [[ OnflowGenerator Range (- rules -) Start ]]
  machineId: [
    required(),
    {
      validator: (_, value, callback) => {
        if (isBlank(value)) {
          callback()
          return
        }
        if (value.length > 50) {
          callback(new Error('服务器id不能超过50个字符'))
        } else {
          callback()
        }
      }
    }
  ],
  ipAddress: [
    required(),
    {
      validator: (_, value, callback) => {
        if (isBlank(value)) {
          callback()
          return
        }
        if (value.length > 64) {
          callback(new Error('服务器ip地址不能超过64个字符'))
        } else {
          callback()
        }
      }
    }
  ],
  macAddress: [
    required(),
    {
      validator: (_, value, callback) => {
        if (isBlank(value)) {
          callback()
          return
        }
        if (value.length > 64) {
          callback(new Error('服务器mac地址不能超过64个字符'))
        } else {
          callback()
        }
      }
    }
  ],
  onlineState: [required()],
  osName: [
    {
      validator: (_, value, callback) => {
        if (isBlank(value)) {
          callback()
          return
        }
        if (value.length > 100) {
          callback(new Error('操作系统名称不能超过100个字符'))
        } else {
          callback()
        }
      }
    }
  ],
  serverName: [
    {
      validator: (_, value, callback) => {
        if (isBlank(value)) {
          callback()
          return
        }
        if (value.length > 100) {
          callback(new Error('服务器名称不能超过100个字符'))
        } else {
          callback()
        }
      }
    }
  ],
  sn: [
    {
      validator: (_, value, callback) => {
        if (isBlank(value)) {
          callback()
          return
        }
        if (value.length > 100) {
          callback(new Error('序列号不能超过100个字符'))
        } else {
          callback()
        }
      }
    }
  ],
  cpuName: [
    {
      validator: (_, value, callback) => {
        if (isBlank(value)) {
          callback()
          return
        }
        if (value.length > 100) {
          callback(new Error('CPU名称不能超过100个字符'))
        } else {
          callback()
        }
      }
    }
  ],
  cpuFrequency: [
    {
      validator: (_, value, callback) => {
        if (isBlank(value)) {
          callback()
          return
        }
        if (value.length > 100) {
          callback(new Error('CPU频率不能超过100个字符'))
        } else {
          callback()
        }
      }
    }
  ],
  physicalProcessorCount: [required()],
  logicalProcessorCount: [required()],
  physicalMemory: [required()],
  virtualMemory: [required()],
  totalStorage: [required()]
  // [[ OnflowGenerator Range (- rules -) End ]]
})

defineExpose({
  submit
})
</script>

<template>
  <Form :rules="rules" @register="formRegister" :schema="schema" ref="formRef" />
</template>
